#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"

trigger-scheduler-docker-local-pre-restore() {
  declare desc="scheduler-docker-local pre-restore plugin trigger"
  declare trigger="pre-restore"
  declare DOKKU_SCHEDULER="$1"

  if [[ "$DOKKU_SCHEDULER" != "docker-local" ]]; then
    return
  fi

  # delete all "old" containers
  # shellcheck disable=SC2046
  "$DOCKER_BIN" container rm $("$DOCKER_BIN" container list --all --format "{{.Names}}" --filter "label=$DOKKU_CONTAINER_LABEL" --quiet | grep -E '(.+\..+\.[0-9]+\.[0-9]+$)') &>/dev/null || true
}

trigger-scheduler-docker-local-pre-restore "$@"
